package com.itheima.demoset;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {

    public static void main(String[] args) {
        // Treeset 自定义比较器方法一
        Teacher t1 = new Teacher("小明", 17, 10000);
        Teacher t2 = new Teacher("小红", 10, 10000);
        Teacher t3 = new Teacher("小刚", 48, 10000);
        Teacher t4 = new Teacher("小刚", 18, 10000);

        Set<Teacher> ts = new TreeSet<>();
        ts.add(t1);
        ts.add(t2);
        ts.add(t3);
        ts.add(t4);
        for (Teacher t : ts){
            System.out.println(t);
        }


        System.out.println("--------------------");
        // Treeset 自定义比较器方法二
        Teacher t5 = new Teacher("小明", 17, 10000);
        Teacher t6 = new Teacher("小红", 10, 10000);
        Teacher t7 = new Teacher("小刚", 48, 10000);
        Teacher t8 = new Teacher("小刚", 18, 10000);
        Set<Teacher> ts2 = new TreeSet<>((o1, o2) -> o2.getAge() - o1.getAge());
        ts2.add(t5);
        ts2.add(t6);
        ts2.add(t7);
        ts2.add(t8);
        for (Teacher t : ts2){
            System.out.println(t);
        }
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Teacher implements Comparable<Teacher>{
    private String name;
    private int age;
    private double money;

    @Override
    public int compareTo(Teacher o) {
        // TODO Auto-generated method stub
        return this.age - o.age;
    }

}